package pe.gob.mpfn.model;

import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "informe")
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "informe")
@RooDbManaged(automaticallyDelete = true)
public class Informe {

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Informe().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countInformes() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Informe o", Long.class).getSingleResult();
    }

	public static List<Informe> findAllInformes() {
        return entityManager().createQuery("SELECT o FROM Informe o", Informe.class).getResultList();
    }

	public static Informe findInforme(Integer intInformeId) {
        if (intInformeId == null) return null;
        return entityManager().find(Informe.class, intInformeId);
    }

	public static List<Informe> findInformeEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Informe o", Informe.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Informe attached = Informe.findInforme(this.intInformeId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Informe merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Informe merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "int_informe_id", columnDefinition = "INT")
    private Integer intInformeId;

	public Integer getIntInformeId() {
        return this.intInformeId;
    }

	public void setIntInformeId(Integer id) {
        this.intInformeId = id;
    }

	@ManyToOne
    @JoinColumn(name = "int_responsable_id", referencedColumnName = "int_personal_id", nullable = false)
    private Personal intResponsableId;

	@ManyToOne
    @JoinColumn(name = "str_numero_expediente_id", referencedColumnName = "str_numero_expediente_id", nullable = false)
    private Expediente strNumeroExpedienteId;

	@Column(name = "str_numero_informe", columnDefinition = "VARCHAR", length = 12)
    @NotNull
    private String strNumeroInforme;

	@Column(name = "str_conclusiones", columnDefinition = "VARCHAR", length = 200)
    @NotNull
    private String strConclusiones;

	@Column(name = "dat_fecha_registro", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date datFechaRegistro;

	@Column(name = "chr_estado", columnDefinition = "CHAR")
    @NotNull
    private Character chrEstado;

	@Column(name = "txt_informe_pdf", columnDefinition = "TEXT")
    private String txtInformePdf;

	public Personal getIntResponsableId() {
        return intResponsableId;
    }

	public void setIntResponsableId(Personal intResponsableId) {
        this.intResponsableId = intResponsableId;
    }

	public Expediente getStrNumeroExpedienteId() {
        return strNumeroExpedienteId;
    }

	public void setStrNumeroExpedienteId(Expediente strNumeroExpedienteId) {
        this.strNumeroExpedienteId = strNumeroExpedienteId;
    }

	public String getStrNumeroInforme() {
        return strNumeroInforme;
    }

	public void setStrNumeroInforme(String strNumeroInforme) {
        this.strNumeroInforme = strNumeroInforme;
    }

	public String getStrConclusiones() {
        return strConclusiones;
    }

	public void setStrConclusiones(String strConclusiones) {
        this.strConclusiones = strConclusiones;
    }

	public Date getDatFechaRegistro() {
        return datFechaRegistro;
    }

	public void setDatFechaRegistro(Date datFechaRegistro) {
        this.datFechaRegistro = datFechaRegistro;
    }

	public Character getChrEstado() {
        return chrEstado;
    }

	public void setChrEstado(Character chrEstado) {
        this.chrEstado = chrEstado;
    }

	public String getTxtInformePdf() {
        return txtInformePdf;
    }

	public void setTxtInformePdf(String txtInformePdf) {
        this.txtInformePdf = txtInformePdf;
    }
}
